package com.example.jinggai;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.json.JSONException;
import org.json.JSONObject;

public class ChuliActivity extends AppCompatActivity {

    private static MqttClient mqttClient;
    private static MqttConnectOptions mqttConnectOptions;
//处理 开盖申请

    RadioButton rbtn1,rbtn2,rbtn3;
    TextView con;
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chuli);

        btn = findViewById(R.id.btn_login);

        rbtn1 = findViewById(R.id.rb1);
        rbtn2 = findViewById(R.id.rb2);
        rbtn3 = findViewById(R.id.rb3);

        con = findViewById(R.id.conment);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(rbtn1.isChecked()){   //发送申请

                    String topic = "shenqing";
                    String content = "shenqing1";
                    int qos = 0;
                    MqttMessage message = new MqttMessage(content.getBytes());
                    message.setQos(qos);
                    try {
                        mqttClient.publish(topic, message);
                    } catch (MqttException e) {
                        e.printStackTrace();
                    }
                }
                if(rbtn2.isChecked()){

                    String topic = "shenqing";
                    String content = "shenqing2";
                    int qos = 0;
                    MqttMessage message = new MqttMessage(content.getBytes());
                    message.setQos(qos);
                    try {
                        mqttClient.publish(topic, message);
                    } catch (MqttException e) {
                        e.printStackTrace();
                    }
                }
                if(rbtn3.isChecked()){

                    String topic = "shenqing";
                    String content = "shenqing3";
                    int qos = 0;
                    MqttMessage message = new MqttMessage(content.getBytes());
                    message.setQos(qos);
                    try {
                        mqttClient.publish(topic, message);
                    } catch (MqttException e) {
                        e.printStackTrace();
                    }
                }

                Toast.makeText(ChuliActivity.this,"已经发送申请开启",Toast.LENGTH_LONG).show();
            }
        });



        initMqttClient();

    }


    private void initMqttClient() {
        mqttConnectOptions  = new MqttConnectOptions();//MQTT的连接设置
        mqttConnectOptions.setCleanSession(true);//清除连接信息
        mqttConnectOptions.setConnectionTimeout(3);// 设置连接超时时间 单位为秒
        mqttConnectOptions.setKeepAliveInterval(60);//心跳包时间60S
        try { mqttClient = new MqttClient(
                "tcp://114.96.74.180:1883",//连接的地址信息
                "android"+System.currentTimeMillis(),//ClientID,使用当前时间戳
                new MemoryPersistence());//
            mqttClient.setCallback(new MqttCallback() {//回调函数
                @Override//连接断开
                public void connectionLost(Throwable throwable) { }
                @Override//接收到消息
                public void messageArrived(String s, MqttMessage mqttMessage)  {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            String source = mqttMessage.toString();

                            try {
                                con.setText(source);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }


                        }
                    });
                }
                @Override//没用过
                public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { }
            });




            new Thread(new Runnable() {//用任务
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        mqttClient.connect(mqttConnectOptions);
                        if (mqttClient.isConnected()) {
                            Log.e("Mqtt Info", "ConnectSuccess");
                            mqttClient.subscribe("conment",0);//订阅conment,得到同意 后，显示到结果
                        }
                    }catch (Exception e){
                        Log.e("Mqtt Err", e.toString());
                    }
                }
            }).start();




        }
        catch (Exception e){
            Log.e("Mqtt Err", e.toString());
        }


    }
}